package com.ruoyi.dangerControl.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dangerControl.dto.SysDepartDto;
import com.ruoyi.dangerControl.vo.SysDepartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dangerControl.mapper.SysDepartMapper;
import com.ruoyi.dangerControl.domain.SysDepart;
import com.ruoyi.dangerControl.service.ISysDepartService;

/**
 * 组织机构Service业务层处理
 *
 * @author lth
 * @date 2023-12-14
 */
@Service
public class SysDepartServiceImpl implements ISysDepartService
{
    @Autowired
    private SysDepartMapper sysDepartMapper;

    /**
     * 查询组织机构
     *
     * @param id 组织机构主键
     * @return 组织机构
     */
    @Override
    public SysDepart selectSysDepartById(String id)
    {
        return sysDepartMapper.selectSysDepartById(id);
    }

    /**
     * 查询组织机构列表
     *
     * @param sysDepart 组织机构
     * @return 组织机构
     */
    @Override
    public List<SysDepart> selectSysDepartList(SysDepart sysDepart)
    {
        return sysDepartMapper.selectSysDepartList(sysDepart);
    }

    /**
     * 新增组织机构
     *
     * @param sysDepart 组织机构
     * @return 结果
     */
    @Override
    public int insertSysDepart(SysDepart sysDepart)
    {
        sysDepart.setCreateTime(DateUtils.getNowDate());
        return sysDepartMapper.insertSysDepart(sysDepart);
    }

    /**
     * 修改组织机构
     *
     * @param sysDepart 组织机构
     * @return 结果
     */
    @Override
    public int updateSysDepart(SysDepart sysDepart)
    {
        sysDepart.setUpdateTime(DateUtils.getNowDate());
        return sysDepartMapper.updateSysDepart(sysDepart);
    }

    /**
     * 批量删除组织机构
     *
     * @param ids 需要删除的组织机构主键
     * @return 结果
     */
    @Override
    public int deleteSysDepartByIds(String[] ids)
    {
        return sysDepartMapper.deleteSysDepartByIds(ids);
    }

    /**
     * 删除组织机构信息
     *
     * @param id 组织机构主键
     * @return 结果
     */
    @Override
    public int deleteSysDepartById(String id)
    {
        return sysDepartMapper.deleteSysDepartById(id);
    }

    /**     查询组织树列表
    * @Author lth
    * @Description //TODO
    * @Date 18:58 2023/12/14
    * @Param
    * @return
     **/
    @Override
    public SysDepartDto getDepartList() {
        List<SysDepart> sysDepartList = sysDepartMapper.getDepartList();
        SysDepart parentDepart = sysDepartList.get(0);
        String parentDepartId = parentDepart.getId();
        List<SysDepart> childrenDepartList = sysDepartMapper.getChildren(parentDepartId);

        List<SysDepartDto> childrenDepartDtoList = childrenDepartList.stream().map(childrenDepart -> {
            SysDepartDto childrenDepartDto = new SysDepartDto();
            BeanUtils.copyProperties(childrenDepart, childrenDepartDto);

            childrenDepartDto.setChildren(null);
            return childrenDepartDto;
        }).collect(Collectors.toList());

        SysDepartDto parentDepartDto = new SysDepartDto();
        BeanUtils.copyProperties(parentDepart,parentDepartDto);
        parentDepartDto.setChildren(childrenDepartDtoList);
        return parentDepartDto;
    }

    /**
     * 通过部门ID查询部门内人员列表
     */
    @Override
    public List<SysDepart> getDepartPersonList(SysDepartVo vo) {
        String departId = vo.getDepartId();
        System.out.println("departId"+departId);
        return null;
    }
}
